/*
 * @LastEditTime: 2023-03-31 23:33:43
 * @Method: 
 * @Description: 
 */
import java.util.*;

package CodeTop;

public class Solution199 {
    public List<Integer> rightSideView(TreeNode root) {
        LinkedList<Integer> res = new LinkedList<>();
        if (root == null) {
            return res;
        }
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while (!q.isEmpty()) {
            int size = q.size();
            for (int i = 0;i < size;i++) {
                TreeNode t = q.poll();
                if (t.left != null) {
                    q.offer(t.left);
                }
                if (t.right != null) {
                    q.offer(t.right);
                }
                if (i == size - 1) {
                    res.add(t.val);
                }
            }
        }
        return res;
    }
}
